/*
 /* SQLConsole.java
 *
 * Created on 6 styczeń 2009, 13:40
 */

package DBBrowser_pack;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.filechooser.FileFilter;

/**
 *
 * @author  admin

 */

public class SQLConsole extends javax.swing.JFrame {

    /** Creates new form SQLConsole */
    public SQLConsole() {
        initComponents();
        //manager=new DBManager();
        jTable1.setModel(model);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        wykonaj = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        textArea = new javax.swing.JTextArea();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Interactive SQL");

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setEnabled(false);
        jScrollPane1.setViewportView(jTable1);

        wykonaj.setText("Wykonaj");
        wykonaj.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                wykonajActionPerformed(evt);
            }
        });

        textArea.setColumns(20);
        textArea.setFont(new java.awt.Font("Arial", 0, 12));
        textArea.setRows(5);
        jScrollPane2.setViewportView(textArea);

        jButton1.setText("Otwórz");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("Zapisz");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 434, Short.MAX_VALUE)
                            .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                .addComponent(wykonaj, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                                .addGap(284, 284, 284)))
                        .addGap(15, 15, 15))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addContainerGap(313, Short.MAX_VALUE))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton1)
                    .addComponent(jButton2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 105, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(wykonaj, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 109, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void wykonajActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_wykonajActionPerformed
try{
    String sql1=textArea.getText();
String[] sql=sql1.split(";");
for (int i=0;i<sql.length;i++){
sql[i].concat(";");
    model.setDataVector(DBLogin.getManager().QueryAsk(sql[i]),DBLogin.getManager().columnNames(""));
}
}catch(NullPointerException e){

}
// TODO add your handling code here:
}//GEN-LAST:event_wykonajActionPerformed

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
///////    // TODO add your handling code here:
   javax.swing.JFileChooser fc= new javax.swing.JFileChooser();

  //java.io.FileReader fs; //= new java.io.FileInputStream(f);
 java.io.FileInputStream fis;
  // File f//=new File(s,"sql");
final String ext="sql";
fc.setCurrentDirectory(new File("."));
       fc.setFileFilter(new
         javax.swing.filechooser.FileFilter()
         {
            public boolean accept(File f)
            {
               String name = f.getName().toLowerCase();

               return name.endsWith("." + ext)
                  || f.isDirectory();
            }
            public String getDescription()
            {
               return ext + " files";
            }
         });
fc.showOpenDialog(fc);
   String s="";

 try {
String filename="";
filename=fc.getSelectedFile().getCanonicalPath();
              if(filename.isEmpty()==false){
                 fis=new java.io.FileInputStream(filename);
 int bajt=0;

    while((bajt = fis.read()) != -1)
    {
        s+=(char)bajt;
      
    }
  textArea.setText(s);
              }
            } catch (IOException ex) {
               javax.swing.JOptionPane.showMessageDialog(null, "Błąd otwarcia pliku");
            }

}//GEN-LAST:event_jButton1ActionPerformed

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
java.io.FileOutputStream fos;
   javax.swing.JFileChooser fc= new javax.swing.JFileChooser();

  //java.io.FileReader fs; //= new java.io.FileInputStream(f);

  // File f//=new File(s,"sql");
final String ext="sql";
fc.setCurrentDirectory(new File("."));
       fc.setFileFilter(new
         javax.swing.filechooser.FileFilter()
         {
            public boolean accept(File f)
            {
               String name = f.getName().toLowerCase();

               return name.endsWith("." + ext)
                  || f.isDirectory();
            }
            public String getDescription()
            {
               return ext + " files";
            }
         });
fc.showSaveDialog(fc);
String s=textArea.getText();
String filename="";
try{
filename=fc.getSelectedFile().getCanonicalPath();
if(filename.contains(".sql")==false)
    if(fc.getFileFilter().getDescription().contains("sql")==true)
      filename+=".sql";

}catch(Exception e){
    
}
//System.out.println("przed: "+filename);

//System.out.println("po: "+filename);


        try {
if(filename.isEmpty()==false)
{
 fos = new java.io.FileOutputStream(filename);
 for(int i=0;i<s.length();i++){
     fos.write(s.charAt(i));
 }
 }
       
            } catch (Exception ex) {
                Logger.getLogger(SQLConsole.class.getName()).log(Level.SEVERE, null, ex);
            
            // TODO add your handling code here:
        

            }    // TODO add your handling code here:
}//GEN-LAST:event_jButton2ActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SQLConsole().setVisible(true);
            }
        });
    }
//protected DBManager manager;
protected javax.swing.table.DefaultTableModel model= new javax.swing.table.DefaultTableModel();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextArea textArea;
    private javax.swing.JButton wykonaj;
    // End of variables declaration//GEN-END:variables

}
